package com.itdr.protal.controller;


import com.itdr.common.Const;
import com.itdr.common.ServerResponse;
import com.itdr.pojo.Shipping;
import com.itdr.pojo.vo.UserVO;
import com.itdr.protal.service.IShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author AirZhang
 * @since 2020-10-20
 */
@RestController
@RequestMapping("/address/")
public class ShippingController {
    @Autowired
    IShippingService shippingService;

    /**
     * 新增地址
     * @param httpSession
     * @param sh
     * @return
     */
    @PostMapping("add")
    public ServerResponse add(HttpSession httpSession, Shipping sh){
        // 判断当前用户是否登录
        UserVO u = (UserVO) httpSession.getAttribute("u");
        if (u == null) {
            return ServerResponse.toFail(Const.UserEnum.NO_LOGIN.getCode(), Const.UserEnum.NO_LOGIN.getDesc());
        }

        return shippingService.addOne(u.getId(),sh);
    }

    /**
     * 删除一个地址
     * @param httpSession
     * @param sid
     * @return
     */
    @GetMapping("delete")
    public ServerResponse delete(HttpSession httpSession, Integer sid){
        // 判断当前用户是否登录
        UserVO u = (UserVO) httpSession.getAttribute("u");
        if (u == null) {
            return ServerResponse.toFail(Const.UserEnum.NO_LOGIN.getCode(), Const.UserEnum.NO_LOGIN.getDesc());
        }
        return shippingService.deleteOne(u.getId(),sid);
    }

    /**
     * 修改地址信息
     * @param httpSession
     * @param sh
     * @return
     */
    @PostMapping("update")
    public ServerResponse update(HttpSession httpSession, Shipping sh){
        // 判断当前用户是否登录
        UserVO u = (UserVO) httpSession.getAttribute("u");
        if (u == null) {
            return ServerResponse.toFail(Const.UserEnum.NO_LOGIN.getCode(), Const.UserEnum.NO_LOGIN.getDesc());
        }

        return shippingService.updateOne(u.getId(),sh);
    }

    /**
     * 获取地址详情
     * @param httpSession
     * @param sid
     * @return
     */
    @GetMapping("selectone")
    public ServerResponse selectOne(HttpSession httpSession, Integer sid){
        // 判断当前用户是否登录
        UserVO u = (UserVO) httpSession.getAttribute("u");
        if (u == null) {
            return ServerResponse.toFail(Const.UserEnum.NO_LOGIN.getCode(), Const.UserEnum.NO_LOGIN.getDesc());
        }

        return shippingService.selectOne(u.getId(),sid);
    }

    /**
     * 获取地址列表
     * @param httpSession
     * @param sid
     * @return
     */
    @GetMapping("selectall")
    public ServerResponse selectAll(HttpSession httpSession, Integer sid){
        // 判断当前用户是否登录
        UserVO u = (UserVO) httpSession.getAttribute("u");
        if (u == null) {
            return ServerResponse.toFail(Const.UserEnum.NO_LOGIN.getCode(), Const.UserEnum.NO_LOGIN.getDesc());
        }

        return shippingService.selectAll(u.getId(),sid);
    }

    /**
     * 设置默认地址
     * @param httpSession
     * @param sid
     * @return
     */
    @GetMapping("setdefault")
    public ServerResponse setDefault(HttpSession httpSession, Integer sid){
        // 判断当前用户是否登录
        UserVO u = (UserVO) httpSession.getAttribute("u");
        if (u == null) {
            return ServerResponse.toFail(Const.UserEnum.NO_LOGIN.getCode(), Const.UserEnum.NO_LOGIN.getDesc());
        }

        return shippingService.setDefault(u.getId(),sid);
    }

}

